package com.spring.component.test.proxy;

import java.lang.reflect.InvocationHandler;
import java.lang.reflect.Method;

/**
 * 代理对象的处理类，实现InvocationHandler的invoke方法，实现对代理类新增逻辑处理，需要传入代理的目标对象
 * 
 * @ClassName: ProxyHandler
 * @Description:
 * @author: liushihua
 * @date: 2018/12/28
 **/
public class ProxyHandler implements InvocationHandler {
  private Object target;

  public ProxyHandler(Object target) {
    this.target = target;
  }

  private void beforeAction() {
    System.out.println("程序猿先泡了杯咖啡");
  }

  @Override
  public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
    beforeAction();
    Object result = method.invoke(this.target, args);
    afterAction();
    return result;
  }

  private void afterAction() {
    System.out.println("程序猿喝完了咖啡，可以通宵加班了");
  }
}
